package com.hr.controller.admin;

import com.hr.domain.dto.EditUserDTO;
import com.hr.domain.dto.UserDTO;
import com.hr.domain.query.UserPageResult;
import com.hr.domain.query.UserQueryParams;
import com.hr.domain.vo.UserVO;
import com.hr.service.RoleService;
import com.hr.utils.Result;
import com.hr.service.UserService;
/*import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;*/
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin")
@Tag(name = "后台用户信息管理")
@AllArgsConstructor
public class UserController {
    /**
     * 用户服务
     */
    private final UserService userService;
    /**
     * 角色服务
     */
    private final RoleService roleService;

    /**
     * 获取所有用户
     * @return Result
     */
    @GetMapping("/user/list")
    /*@ApiOperation("获取所有用户")*/
    @Operation(summary = "获取所有用户")
    public Result getAllUsers() {
        log.info("获取所有用户");
        List<UserVO> result = userService.getAllUsers();
        return Result.success(result);
    }

    /**
     * 获取用户分页列表
     * @param  queryParams
     * @return Result
     */
    @GetMapping("/user/page")
    @Operation(summary = "分页获取用户分页列表")
    public Result getUserPageList(UserQueryParams queryParams) {
        log.info("分页获取用户分页列表");
        UserPageResult result = userService.getUserPageList(queryParams);
        return Result.success(result);
    }

    /**
     * 禁用、启用账户
     * @param  id
     * @param  status
     * @return msg
     */
    @PutMapping("/user/status/{id}/{status}")
    @Operation(summary = "禁用、启用账户")
    public Result updateUserStatus(@PathVariable("id") Long id,
                                   @PathVariable("status")Integer status) {
        log.info("禁用、启用账户");
        String msg = userService.updateUserStatus(id, status);
        return Result.success(msg);
    }

    /**
     * 新增用户
     */
    @PostMapping("/user/add")
    @Operation(summary = "新增用户")
    public Result addUser(@RequestBody UserDTO userDTO) {
        if (userDTO == null){
            return Result.error("用户信息不能为空,请重新输入！！！");
        }
        log.info("新增用户");
        String msg = userService.addUser(userDTO);
        return Result.success(msg);
    }


    /**
     * 获取用户角色
     * @return Result
     */
    @GetMapping("/user/role")
    @Operation(summary = "获取用户角色")
    public Result getUserRole() {
        log.info("获取用户角色");
        List<String> result = roleService.getUserRole();
        return Result.success(result);
    }
    /**
     * 获取单条用户信息（查询回显）
     * @return Result
     */
    @GetMapping("/user/info/{id}")
    @Operation(summary = "获取单条用户信息（查询回显）")
    public Result getUserInfo(@PathVariable("id") Long id) {
        log.info("获取单条用户信息（查询回显）");
        UserVO userVO = userService.SelectById(id);
        if (userVO == null){
            return Result.error("用户不存在");
        }
        return Result.success(userVO);
    }
    /**
     * 修改用户信息
     * @return Result
     */
    @PutMapping("/user/update")
    @Operation(summary = "修改用户信息")
    public Result updateUser(@RequestBody EditUserDTO editUserDTO) {
        if (editUserDTO == null){
            return Result.error("用户信息不能为空,请重新输入！！！");
        }
        log.info("修改用户信息");
        String msg = userService.updateUser(editUserDTO);
        return Result.success(msg);
    }



}
